package com.shangxia.monomer.study.math.algorithm.sort;

import com.alibaba.fastjson.JSONObject;

/**
 * @author zhouya38158
 * @Package com.shangxia.monomer.study.math.algorithm.sort
 * @date 2021/7/21 15:33
 */
public class InsertionSort {
    public static void main(String[] args) {
        int[] arr = {2,3,5,7,9,4,1,8,6};
        System.out.println(JSONObject.toJSONString(arr));
        insertionSort(arr);
        System.out.println(JSONObject.toJSONString(arr));
        
    }

    public static void insertionSort(int[] arr) {
        for (int i = 1; i < arr.length; i++) {
            int tmp = arr[i];
            int j = i - 1;

            while (j >= 0 && arr[j] > tmp) {
                arr[j + 1] = arr[j];
                j--;
            }
            arr[j + 1] = tmp;
        }
    }
}
